angular.module('Controller')

.controller('QgyRepairManageCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            apartmentId:$localStorage['accountStore'],
            filter:undefined
            status:[]
            startTime:undefined
            endTime:undefined
        }
#    $scope.printItem={
#        name:'张三'
#        phone:'13812341234'
#        content:'空调不制冷'
#    }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.payWayList=commonConstant.payWayList(true)
    $scope.repairStatusList=commonConstant.repairStatusList(true)
    $scope.needFeeList=[
        {value:true,name:'是'},
        {value:false,name:'否'}
    ]
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )

    #根据传入的动作和要操作的id更新Array
    $scope.updateSelection=($event, id)->
        checkbox = $event.target;
        if checkbox.checked && $scope.search.status.indexOf(id) == -1
            $scope.search.status.push(id)
        if !checkbox.checked && $scope.search.status.indexOf(id) != -1
            idx = $scope.search.status.indexOf(id)
            $scope.search.status.splice(idx, 1)

    #返回true false
    $scope.isSelected=(id)->
        return $scope.search.status.indexOf(id) >= 0

    $scope.doQuery=($defer, params)->
        status = undefined
        if $scope.search.status.length > 0
            status = $scope.search.status.join(",")
        reqData={
            page: params.page() - 1,
            size: params.count(),
            buzProduct:$scope.search.apartmentId,
            filter:$scope.search.filter,
            status:status
        }
        reqData.startTime=moment($scope.search.startTime).format('YYYY-MM-DD') if $scope.search.startTime
        reqData.endTime=moment($scope.search.endTime).format('YYYY-MM-DD') if $scope.search.endTime
        $http.get('/repair',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={type:1,enabled:1}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            showModal('edit',theData)
        dealData:(item)->
            theData=angular.copy(item)
            theData.providerTime=new Date(theData.providerTime) if(theData.providerTime)
            showModal('deal',theData)
        viewData:(item)->
            theData=angular.copy(item)
            theData.providerTime=new Date(theData.providerTime) if(theData.providerTime)
            showModal('view',theData)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        removeData:(item)->
            $http.delete('/repair/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('报修信息删除成功');
                    $scope.queryData();
                else
                    wfAlert.toastError(resp.message)
            )
        printData:(item)->
            $scope.printItem=angular.copy(item)
            _.defer(()->
                $('#repair-print-container').print({title:'报修单_'+item.uuid})
                $scope.printItem=undefined
            )
            ''
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            size:'lg',
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyRepairManageEditCtrl',
            templateUrl: 'templates/business-manage/repair-manage/repair-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)